package py.com.erpone.session.dm;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import py.com.erpone.entity.dm.Empresa;


@Stateful
@Named
@LocalBean
@ConversationScoped
public class EmpresaBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Empresa empresa;
	
	
	public EmpresaBean(){
		
	}


	public Empresa getEmpresa() {
		return empresa;
	}


	public void setEmpresa(Empresa empresa) {
		this.empresa = empresa;
	}
	
	public String iniciar(){
		if(conversation.isTransient())
			conversation.begin();
		empresa=new Empresa();
		return "/dm/empresa.xhtml";
	}
	
	public String iniciar(Empresa empresa){
		if(conversation.isTransient())
			conversation.begin();
		this.empresa=empresa;
		return "/dm/empresa.xhtml";
	}
	
	
	public String guardar(){
		if(empresa.getId()==null)
			em.persist(empresa);
		else
			em.merge(empresa);
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/empresas.xhtml";
	}
	
	public String eliminar(Empresa empresa){
		empresa.setActivo(false);
		em.merge(empresa);
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/empresas.xhtml";
	}
	
	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/empresas.xhtml";
	}
	
	
	
}
